{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "0eb5994e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "已保存合并数据到 'merged_df.csv'\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import pandas as pd\n",
    "\n",
    "\n",
    "\n",
    "# 读取相同CSV文件，假设这些文件内容有所不同\n",
    "df1 = pd.read_csv('emails1.csv')\n",
    "df2 = pd.read_csv('spam1.csv')\n",
    "df3 = pd.read_csv('email1.csv')\n",
    "#df4 = pd.read_csv('combined_data1.csv')\n",
    "df5 = pd.read_csv('enronSpamSubset1.csv')\n",
    "\n",
    "\n",
    "# 合并所有处理后的 DataFrame\n",
    "merged_df = pd.concat([df1, df2, df3,  df5], ignore_index=True)\n",
    "merged_df.columns = ['text', 'spam']\n",
    "# 保存合并后的 DataFrame\n",
    "merged_df.to_csv('merged_dfs.csv', index=False)\n",
    "print(\"已保存合并数据到 'merged_df.csv'\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "8dd1429a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在加载并处理数据...\n",
      "正在保存处理后的数据...\n",
      "数据已保存到 lingSpam1.csv\n",
      "程序执行完毕。\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "def unify_label(label):\n",
    "    \"\"\"\n",
    "    统一标签格式，将 'spam' 转换为 1，'ham' 转换为 0\n",
    "    :param label: 标签值，可能是 'spam', 'ham', 1, 或 0\n",
    "    :return: 统一后的标签值（1 或 0）\n",
    "    \"\"\"\n",
    "    if label == 'spam' or label == 1:\n",
    "        return 1\n",
    "    elif label == 'ham' or label == 0:\n",
    "        return 0\n",
    "    else:\n",
    "        return None  # 如果标签无效，返回 None\n",
    "\n",
    "def load_and_process(file_path):\n",
    "    \"\"\"\n",
    "    读取 CSV 文件，筛选符合条件的样本并统一标签格式\n",
    "    :param file_path: 数据文件路径\n",
    "    :return: 处理后的 DataFrame\n",
    "    \"\"\"\n",
    "    # 读取 CSV 文件\n",
    "    df = pd.read_csv(file_path)\n",
    "\n",
    "    # 选择前两列并重命名列名\n",
    "    df = df.iloc[:, 1:3].copy()  # 只保留前两列\n",
    "    #df.iloc[:, [0, 1]] = df.iloc[:, [1, 0]].values\n",
    "    df.columns = ['text', 'label']  # 重命名列为 'text' 和 'label'\n",
    "\n",
    "    # 筛选 label 列为 'spam', 'ham', 1 或 0 的行\n",
    "    #df = df[df['label'].isin(['spam', 'ham', 1, 0])]\n",
    "\n",
    "    # 统一标签格式：将 'spam' -> 1, 'ham' -> 0\n",
    "    df['label'] = df['label'].apply(unify_label)\n",
    "\n",
    "    # 删除标签无效（即 None）或者缺失值的行\n",
    "    df = df.dropna(subset=['label'])\n",
    "\n",
    "    df = df[df['label'].isin([1, 0])]\n",
    "\n",
    "    return df\n",
    "\n",
    "def save_data(df, output_path):\n",
    "    \"\"\"\n",
    "    将处理后的 DataFrame 保存为 CSV 文件\n",
    "    :param df: 要保存的数据\n",
    "    :param output_path: 保存文件的路径\n",
    "    \"\"\"\n",
    "    df.to_csv(output_path, index=False)\n",
    "    print(f\"数据已保存到 {output_path}\")\n",
    "\n",
    "def main():\n",
    "    # 输入文件路径和输出文件路径\n",
    "    input_file_path = 'lingSpam.csv'  # 替换为实际路径\n",
    "    output_file_path = 'lingSpam1.csv'  # 处理后的数据保存路径\n",
    "\n",
    "    # 加载并处理数据\n",
    "    print(\"正在加载并处理数据...\")\n",
    "    processed_data = load_and_process(input_file_path)\n",
    "\n",
    "    # 保存处理后的数据\n",
    "    print(\"正在保存处理后的数据...\")\n",
    "    save_data(processed_data, output_file_path)\n",
    "\n",
    "    print(\"程序执行完毕。\")\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "716cf23c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据已保存到 email1.csv\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "def load_and_process(file_path):\n",
    "    # 读取 CSV 文件\n",
    "    df = pd.read_csv(file_path)\n",
    "\n",
    "    # 假设第四列的名字是 'column_name'，你可以通过位置索引获取该列\n",
    "    # 如果第四列不为零，则删除该行\n",
    "    df = df[df.iloc[:, 1].isin([0, 1,'spam','ham'])]\n",
    " # 通过位置索引，第四列是 `iloc[:, 3]`（索引从 0 开始）\n",
    "    \n",
    "    return df\n",
    "def save_data(df, output_path):\n",
    "    \"\"\"\n",
    "    将处理后的 DataFrame 保存为 CSV 文件\n",
    "    :param df: 要保存的数据\n",
    "    :param output_path: 保存文件的路径\n",
    "    \"\"\"\n",
    "    df.to_csv(output_path, index=False)\n",
    "    print(f\"数据已保存到 {output_path}\")\n",
    "# 调用示例\n",
    "def main():\n",
    "    file_path = 'email.csv'\n",
    "    output_file_path = 'email1.csv'  # 处理后的数据保存路径\n",
    "    df = load_and_process(file_path)\n",
    "    save_data(df, output_file_path)\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch_gpu",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
